package com.ruoyi.gcgl.service.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.gcgl.domain.SjChance;
import com.ruoyi.gcgl.mapper.SjChanceMapper;
import com.ruoyi.gcgl.service.ISjChanceService;

/**
 * 商机跟踪Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-04
 */
@Service
public class SjChanceServiceImpl implements ISjChanceService 
{
    @Autowired
    private SjChanceMapper sjChanceMapper;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Resource
    IdentityService identityService;

    /**
     * 查询商机跟踪
     * 
     * @param id 商机跟踪主键
     * @return 商机跟踪
     */
    @Override
    public SjChance selectSjChanceById(Long id)
    {
        return sjChanceMapper.selectSjChanceById(id);
    }

    /**
     * 查询商机跟踪列表
     * 
     * @param sjChance 商机跟踪
     * @return 商机跟踪
     */
    @Override
    public List<SjChance> selectSjChanceList(SjChance sjChance)
    {
        return sjChanceMapper.selectSjChanceList(sjChance);
    }

    /**
     * 新增商机跟踪
     * 
     * @param sjChance 商机跟踪
     * @return 结果
     */
    @Override
    public int insertSjChance(SjChance sjChance)
    {
    	int rows = sjChanceMapper.insertSjChance(sjChance);
    	// 发起合同审核流程
        identityService.setAuthenticatedUserId(sjChance.getOwner());
        HashMap<String, Object> variables = new HashMap<>();
        variables.put("applyuserid", sjChance.getOwner());
        variables.put("level1", sjChance.getXmds());
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("sjshangjilixiang", String.valueOf(sjChance.getId()), variables);
    	
    	SjChance obj = new SjChance();
    	obj.setId(sjChance.getId());
    	obj.setProcessInstanceId(pi.getProcessInstanceId());
    	sjChanceMapper.updateSjChance(obj);
    	
        return rows;
    }

    /**
     * 修改商机跟踪
     * 
     * @param sjChance 商机跟踪
     * @return 结果
     */
    @Override
    public int updateSjChance(SjChance sjChance)
    {
    	// 发起合同审核流程
//        identityService.setAuthenticatedUserId(sjChance.getOwner());
//        HashMap<String, Object> variables = new HashMap<>();
//        variables.put("applyuserid", sjChance.getOwner());
//        variables.put("level1", sjChance.getApprovename());
//        ProcessInstance pi = runtimeService.startProcessInstanceByKey("sjshangjilixiang", String.valueOf(sjChance.getId()), variables);
//        sjChance.setProcessInstanceId(pi.getProcessInstanceId());
    	
        return sjChanceMapper.updateSjChance(sjChance);
    }
    
    @Override
    public int updateSjChance4Check(SjChance sjChance)
    {
        return sjChanceMapper.updateSjChance(sjChance);
    }

    /**
     * 批量删除商机跟踪
     * 
     * @param ids 需要删除的商机跟踪主键
     * @return 结果
     */
    @Override
    public int deleteSjChanceByIds(String ids)
    {
        return sjChanceMapper.deleteSjChanceByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除商机跟踪信息
     * 
     * @param id 商机跟踪主键
     * @return 结果
     */
    @Override
    public int deleteSjChanceById(Long id)
    {
        return sjChanceMapper.deleteSjChanceById(id);
    }
}
